﻿import com.dinmax.flash.utilities.timer.*
import com.dinmax.flash.utilities.communication.TextParserListener
import com.dinmax.flash.utilities.communication.TextParser
import com.dinmax.flash.utilities.xml.XMLAction
import com.dinmax.flash.central.communication.*
import com.dinmax.flash.central.*
import com.dinmax.flash.central.action.*

class Communication extends XMLSocket implements TimerListener {
	
private var IP:String;
private var PORT:Number;
private static var instance:Communication;

private var send:Boolean = false;

private var listener:CommunicationEventListener;

public static function getActive():Communication {
	return instance;
}

public static function newInstance(listener:CommunicationEventListener):Void {
	instance=new Communication(listener);
}

function Communication(listener:CommunicationEventListener) {
	this.listener=listener;
	this.getData();

}

public function getData():Void {
	var root:Communication = this;
	var parser:TextParserListener= new TextParserListener();
	parser.parseError=function():Void {
		trace("parseError");
	};
	parser.parseFinished= function(text:String):Void {
		var t:String = text;
		var divisor:Array = t.split("\n");
		//trace(divisor);
		for (var i:Number=0; i< divisor.length; i++) {
			var data:Object=new Object();
			var atts:Array= divisor[i].split("|");
			for(var j:Number=0; j < atts.length; j++){
				var values:Array = atts[j].split("=");
				switch(values[0]){
					case "IP":
					root.setIP(values[1]);
					break;
					case "PORT":
					root.setPORT(values[1]);
					break;
				};
			};
												
		}
		
	};
	var textParser:TextParser = new TextParser("data.zro", parser);	
};

public function getIP():String {
	return IP;
}

public function setIP(IP:String):Void {
	this.IP = IP;
	return;
}

public function getPORT():Number {
	return PORT;
}

public function setPORT(PORT:Number):Void {
	this.PORT = PORT;
	return;
}

public function doCommunication():Boolean {
 	//trace("Communication.as doCommunication()");
 	return super.connect(this.IP, this.PORT);
}; //fin funci

//
// ACTIONS
//

public function sendAction(act:XMLAction):Void {
	trace("\n--- Communication.as | sendAction() name= "+act.getName()+"\n"+act.toXML());
	super.send(new XML(act.toXML()));
	return;
};

//
//


function onXML(doc:XML):Void {
	
	doc.ignoreWhite = true;
	trace("-----\nCommunication.as | onXML() | doc\n\n"+doc+"\n\n-----");
	//Timer.getActive().finishTimer();
	try {
		listener.event(CommunicationEventFactory.getComunicationEvent(doc));
	} catch (e) {
		listener.eventException(e);
	};
	return;
}

function onConnect(success:Boolean):Void {
	trace("Communication.as onConnect()= "+success);
	if(!success){
		//Timer.getActive().finishTimer();
		//getExtensions();
	} else {
		//registerMachine(SGS.getActive().getSerial());
	};
	return;
};

function onClose():Void{
	trace("Communication.as | 144 | onClose()");
	//listener.eventException(new CommunicationLostException());
	return;
};

function timeOut():Void{
	//trace("Communication.as | 150 | timeOut()= mucho tiempo de espera");
	//listener.eventException(new CommunicationTimeOutException());
	return;
};

}